const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zhRadio: true,
    wxRadio: false,
    price: '',
    title: '',
    balanceVal: '',
    id: '',
    order_id: '',
    passwordVal: '',
    payPasswordBox: false,
    code: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      price: options.cash,
      title: options.title,
      order_id: options.orderid,
      code: options.code
    })
    // 钱包余额
    this.balance();
  },
  // 支付选择
  radioChange: function (e) {
    var { wxRadio, zhRadio } = this.data
    this.setData({
      wxRadio: !wxRadio,
      zhRadio: !zhRadio
    })
  },
  // 钱包余额
  balance: function () {
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'pay/balance',
      data: {
        token: app.globalData.token
      },
      success(res) {
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            balanceVal: res.data.data.balance
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 支付
  submitPay: function () {
    var { zhRadio, payPasswordBox, order_id } = this.data
    var _this = this;
    if (zhRadio) {
      this.setData({
        payPasswordBox: !payPasswordBox
      })
    } else {
      var _this = this
      var url = app.globalData.baseUrl + 'pay/wechatPay1';
      var { code } = this.data;
      var data = {
        code: code,
        token: app.globalData.token
      }
      wx.showLoading({
        title: '加载中',
      })
      app.wxPostRequest(url, data, (res) => {
        console.log(res)
        wx.hideLoading()
        if (res.code == 1000) {
          wx.requestPayment({
            timeStamp: String(res.data.timeStamp),
            nonceStr: String(res.data.nonce_str),
            package: String(res.data.package),
            signType: 'MD5',
            paySign: String(res.data.paySign),
            success(payres) {
              console.log(payres)
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 1500
              })
              setTimeout(function(){
                wx.redirectTo({
                  url: '/taskPage/pages/worksConfirm/worksConfirm?order_id=' + order_id,
                })
              },1500)
            },
            fail(payres) {
              console.log(payres)
            }
          })
        }
      }, (err) => {
        wx.hideLoading()
        wx.showToast({
          title: err.data.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  // 支付密码
  payPasswordInp: function (e) {
    this.setData({
      passwordVal: e.detail.value
    })
  },
  // 取消
  cancelPay: function () {
    var { payPasswordBox } = this.data;
    this.setData({
      payPasswordBox: !payPasswordBox
    })
  },
  // 确定
  determinePay: function () {
    var _this = this
    var { id, order_id, passwordVal } = this.data;
    if (passwordVal == '') {
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/cashPayGoods',
      data: {
        order_id: order_id,
        pay_type: 0,
        password: passwordVal,
        token: app.globalData.token
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        // console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000
          })
          _this.setData({
            payPasswordBox:false
          })
          setTimeout(function () {
            wx.redirectTo({
              url: '/taskPage/pages/worksConfirm/worksConfirm?order_id=' + order_id,
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
